package com.cx.bank.dao;

import com.cx.bank.manager.ManagerImpl;
import com.cx.bank.model.MoneyBean;
import com.cx.bank.model.UserBean;
import com.cx.bank.util.MD5;

import java.io.*;
import java.util.Properties;

/**
 * @author jxau_scf
 * @date 2021/7/14 - 9:19
 */
public class BankDaoImpl implements BankDaoInterface {
    @Override
    public boolean saveMoney(String userName, MoneyBean moneyBean) throws IOException {
        File file = new File("src/main/resources/"+userName+".properties");
        if (!file.exists()){
            System.out.println(userName+".properties"+"文件不存在");
            return false;
        }
        Properties properties = new Properties();
        properties.setProperty("money", moneyBean.getMoney().toString());
        FileWriter fileWriter = new FileWriter(file, true);
        properties.store(fileWriter, "update money data");
        fileWriter.close();
        return true;
    }

    /**
     * 添加用户
     * @param userBean
     * @throws IOException
     */
    @Override
    public boolean insertUser(UserBean userBean) throws IOException {
        //md5加密
//        MD5 md5 = new MD5();
        Properties properties = new Properties();
//        properties.setProperty("password", MD5.getMD5(userBean.getPassword().toString()));
        properties.setProperty("password", userBean.getPassword().toString());
        properties.setProperty("money", userBean.getMoneyBean().getMoney().toString());
        properties.setProperty("userName", userBean.getUserName());
//        FileWriter fileWriter = new FileWriter("src/main/resources/bank.properties",true);
        FileWriter fileWriter = new FileWriter("src/main/resources/"+userBean.getUserName()+".properties");
        properties.store(fileWriter, "save data");
        fileWriter.close();
        return true;


    }

    /**
     * 更新余额
     * @param userName
     * @param money
     * @return
     * @throws IOException
     */
    @Override
    public boolean updateMoney(String userName, double money) throws IOException {
        File file = new File("src/main/resources/"+userName+".properties");
        if (!file.exists()){
            System.out.println(userName+".properties"+"文件不存在");
            return false;
        }
        Properties properties = new Properties();
//        FileInputStream fis = new FileInputStream(file);
//        properties.load(fis);
//        double moneyInProperties = Double.parseDouble(properties.getProperty("money"));
        properties.setProperty("money", String.valueOf(money));
        FileWriter fileWriter = new FileWriter(file, true);
        properties.store(fileWriter, "update"+ userName +" data");
        fileWriter.close();
        return true;
    }

    /**
     * 根据用户名查找用户
     * @param userName
     * @return
     * @throws IOException
     */
    @Override
    public UserBean findByName(String userName) throws IOException {
        File file = new File("src/main/resources/"+userName+".properties");
        if (!file.exists()){
//            System.out.println(userName+".properties"+"文件不存在");
            return null;
        }
        FileInputStream fis = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(fis);
        String userNameInProperties = properties.getProperty(userName);
        Integer passwordInProperties = Integer.parseInt(properties.getProperty("password"));
        double moneyInProperties = Double.parseDouble(properties.getProperty("money"));
        UserBean userBean = new UserBean(passwordInProperties,new MoneyBean(moneyInProperties), userNameInProperties);
        return userBean;
    }

    /**
     * 根据用户名和密码查找用户
     * @param userName
     * @param password
     * @return
     * @throws IOException
     */
    @Override
    public UserBean findUser(String userName, Integer password) throws IOException {
        if (userName == null || password ==null){
            System.out.println("userName或password为空");
            return null;
        }
        File file = new File("src/main/resources/"+userName+".properties");
        if (!file.exists()){
            System.out.println(userName+".properties"+"文件不存在");
            return null;
        }
        FileInputStream fis = new FileInputStream(file);
        Properties properties = new Properties();
        properties.load(fis);
        String userNameInProperties = properties.getProperty(userName);
        Integer passwordInProperties = Integer.parseInt(properties.getProperty("password"));
        if (!password.equals(passwordInProperties)){
            System.out.println("密码错误");
            return null;
        }
        double moneyInProperties = Double.parseDouble(properties.getProperty("money"));
        UserBean userBean = new UserBean(passwordInProperties,new MoneyBean(moneyInProperties), userNameInProperties);
        return userBean;
    }


    @Override
    public void addBank(String userName, MoneyBean moneyBean) throws IOException {
        boolean status = saveMoney(userName, moneyBean);
        if (status){
            System.exit(1);
        }else {
            System.out.println("退出系统失败");
        }

    }
}
